package com.jcl.cloud.services.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.jcl.cloud.common.service.exception.JclApiException;
import com.jcl.cloud.common.service.exception.JclCrawlException;
import com.jcl.cloud.services.gateway.common.task.CrawlTaskReq;
import com.jcl.cloud.services.gateway.common.task.CrawlTaskRsp;
import com.jcl.cloud.services.gateway.common.task.TaskContent;
import com.jcl.cloud.services.gateway.common.task.BuyResumeReq;
import com.jcl.cloud.services.gateway.common.task.BuyResumeRsp;
import com.jcl.cloud.services.gateway.service.BuyResumeTaskService;
import com.jcl.cloud.services.gateway.service.CrawlerTaskService;
import com.jcl.cloud.services.gateway.util.AccountUtils;
import com.netflix.astyanax.util.TimeUUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping({"/job/v1"})
@CrossOrigin
public class TaskController {


    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private CrawlerTaskService crawlerTaskService;
    @Autowired
    private BuyResumeTaskService buyResumeTaskService;

    @RequestMapping(
            value = {"/tasks"},
            method = {RequestMethod.POST}
    )
    public CrawlTaskRsp createTask(@RequestBody CrawlTaskReq crawlTaskReq) throws JclApiException {

        try {
            String traceId = crawlTaskReq.getTraceId();
            String taskId = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
            String tenantId = crawlTaskReq.getTenantId();
            String account = crawlTaskReq.getAccount();
            String userName = crawlTaskReq.getUserName();
            String passWord = crawlTaskReq.getPassWord();
            String siteCode = crawlTaskReq.getSiteCode();
            String taskType = crawlTaskReq.getTaskType();
            String cron = crawlTaskReq.getCron();
            String level = crawlTaskReq.getLevel();

            //后期可以对请求进行监控处理 ip  账号
            TaskContent taskContent = new TaskContent();
            taskContent.setTraceId(traceId);
            taskContent.setTaskId(taskId);
            taskContent.setSiteCode(siteCode);
            taskContent.setTaskType(taskType);
            taskContent.setTenantId(tenantId);
            taskContent.setAccount(account);
            taskContent.setUserName(userName);
            taskContent.setPassWord(passWord);
            taskContent.setAccountKey(AccountUtils.getAccountKey(account, userName, passWord));
            taskContent.setCron(cron);
            taskContent.setLevel(level);



            CrawlTaskRsp crawlTaskRsp = this.crawlerTaskService.createTask(taskContent);
            return crawlTaskRsp;
        } catch (JclCrawlException e) {
            throw new JclApiException();
        } finally {

        }
    }





    @RequestMapping(
            value = {"/buy/tasks"},
            method = {RequestMethod.POST}
    )
    public BuyResumeRsp createBuyResumeTask(@RequestBody BuyResumeReq buyResumeReq){
        try {

            logger.info("接收到的购买简历请求为buyResumeReq：" + JSON.toJSONString(buyResumeReq) );
            BuyResumeRsp buyResumeRsp = this.buyResumeTaskService.createBuyResumeTask(buyResumeReq);

            return buyResumeRsp;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }

        return null;
    }

}
